import { db } from '@/lib/db';
import { orders, orderItems, customers, products } from '@/lib/schema';
import { eq } from 'drizzle-orm';

async function generateSampleOrders() {
  try {
    console.log('开始生成示例订单数据...');

    // 获取所有客户
    const allCustomers = await db.select().from(customers);
    if (allCustomers.length === 0) {
      console.log('没有客户数据，请先创建客户');
      return;
    }

    // 获取所有产品
    const allProducts = await db.select().from(products);
    if (allProducts.length === 0) {
      console.log('没有产品数据，请先创建产品');
      return;
    }

    // 删除现有订单数据
    await db.delete(orderItems);
    await db.delete(orders);
    console.log('已清除现有订单数据');

    const orderStatuses = ['pending', 'paid', 'shipped', 'delivered', 'cancelled'] as const;
    const sampleOrders = [
      {
        customerId: allCustomers[0].id,
        totalAmount: 2999,
        status: 'pending' as const,
        items: [
          { productId: allProducts[0].id, quantity: 2, price: 999, subtotal: 1998 },
          { productId: allProducts[1].id, quantity: 1, price: 1001, subtotal: 1001 }
        ]
      },
      {
        customerId: allCustomers[1].id,
        totalAmount: 1580,
        status: 'paid' as const,
        items: [
          { productId: allProducts[2].id, quantity: 1, price: 1580, subtotal: 1580 }
        ]
      },
      {
        customerId: allCustomers[2].id,
        totalAmount: 4999,
        status: 'shipped' as const,
        items: [
          { productId: allProducts[0].id, quantity: 1, price: 999, subtotal: 999 },
          { productId: allProducts[3].id, quantity: 2, price: 2000, subtotal: 4000 }
        ]
      },
      {
        customerId: allCustomers[3].id,
        totalAmount: 899,
        status: 'delivered' as const,
        items: [
          { productId: allProducts[4].id, quantity: 1, price: 899, subtotal: 899 }
        ]
      },
      {
        customerId: allCustomers[4].id,
        totalAmount: 2599,
        status: 'cancelled' as const,
        items: [
          { productId: allProducts[1].id, quantity: 1, price: 1001, subtotal: 1001 },
          { productId: allProducts[2].id, quantity: 1, price: 1598, subtotal: 1598 }
        ]
      },
      {
        customerId: allCustomers[0].id,
        totalAmount: 3598,
        status: 'delivered' as const,
        items: [
          { productId: allProducts[0].id, quantity: 3, price: 999, subtotal: 2997 },
          { productId: allProducts[5].id, quantity: 1, price: 601, subtotal: 601 }
        ]
      },
      {
        customerId: allCustomers[1].id,
        totalAmount: 2100,
        status: 'paid' as const,
        items: [
          { productId: allProducts[3].id, quantity: 1, price: 2100, subtotal: 2100 }
        ]
      },
      {
        customerId: allCustomers[2].id,
        totalAmount: 1299,
        status: 'pending' as const,
        items: [
          { productId: allProducts[6].id, quantity: 1, price: 1299, subtotal: 1299 }
        ]
      }
    ];

    for (const orderData of sampleOrders) {
      // 创建订单
      const [order] = await db.insert(orders).values({
        customerId: orderData.customerId,
        totalAmount: orderData.totalAmount,
        status: orderData.status
      }).returning();

      // 创建订单项
      for (const item of orderData.items) {
        await db.insert(orderItems).values({
          orderId: order.id,
          productId: item.productId,
          quantity: item.quantity,
          price: item.price,
          subtotal: item.subtotal
        });
      }

      console.log(`创建订单 #${order.id}，状态: ${orderData.status}，金额: ¥${orderData.totalAmount}`);
    }

    console.log('示例订单数据生成完成！');
    console.log(`共创建了 ${sampleOrders.length} 个订单`);

  } catch (error) {
    console.error('生成示例订单数据时出错:', error);
  }
}

// 运行脚本
generateSampleOrders()
  .then(() => {
    console.log('脚本执行完成');
    process.exit(0);
  })
  .catch((error) => {
    console.error('脚本执行出错:', error);
    process.exit(1);
  });